@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

REM 清理构建脚本 (Windows版本)
REM 作者: auth-cloud
REM 日期: 2024-01-01

REM 项目根目录
set "PROJECT_ROOT=%~dp0.."
set "PROJECT_ROOT=%PROJECT_ROOT:~0,-1%"
echo 项目根目录: %PROJECT_ROOT%

REM 清理Maven缓存
:clean_maven_cache
echo 清理Maven缓存...
mvn dependency:purge-local-repository -DmanualInclude="org.projectlombok:lombok"
echo Maven缓存清理完成

REM 清理项目构建文件
:clean_project
echo 清理项目构建文件...
cd /d "%PROJECT_ROOT%"

REM 删除target目录
for /d /r . %%d in (target) do @if exist "%%d" rd /s /q "%%d"

REM 删除编译缓存
del /s /q .classpath 2>nul
del /s /q .project 2>nul
for /d /r . %%d in (.settings) do @if exist "%%d" rd /s /q "%%d"

echo 项目构建文件清理完成

REM 重新编译项目
:rebuild_project
echo 重新编译项目...
cd /d "%PROJECT_ROOT%"

REM 使用Maven包装器编译
if exist "mvnw.cmd" (
    call mvnw.cmd clean compile -DskipTests
) else (
    call mvn clean compile -DskipTests
)

if errorlevel 1 (
    echo 项目编译失败
    exit /b 1
) else (
    echo 项目编译成功
)

REM 检查Java版本
:check_java_version
echo 检查Java版本...
java -version >nul 2>&1
if errorlevel 1 (
    echo 错误: 未找到Java环境
    exit /b 1
) else (
    echo 当前Java版本:
    java -version
)

REM 检查Maven版本
:check_maven_version
echo 检查Maven版本...
mvn -version >nul 2>&1
if errorlevel 1 (
    echo 错误: 未找到Maven环境
    exit /b 1
) else (
    echo 当前Maven版本:
    mvn -version
)

REM 显示帮助信息
:show_help
echo.
echo 用法: %~nx0 [命令]
echo.
echo 命令:
echo   clean-cache    清理Maven缓存
echo   clean-project  清理项目构建文件
echo   rebuild        清理并重新编译项目
echo   check-env      检查Java和Maven环境
echo   all            执行所有清理和检查操作
echo   help           显示帮助信息

REM 主函数
:main
if "%1"=="clean-cache" goto clean_maven_cache_cmd
if "%1"=="clean-project" goto clean_project_cmd
if "%1"=="rebuild" goto rebuild_cmd
if "%1"=="check-env" goto check_env_cmd
if "%1"=="all" goto all_cmd
if "%1"=="help" goto show_help
if "%1"=="-h" goto show_help
if "%1"=="--help" goto show_help
goto show_help

:clean_maven_cache_cmd
call :clean_maven_cache
goto end

:clean_project_cmd
call :clean_project
goto end

:rebuild_cmd
call :check_java_version
call :check_maven_version
call :clean_project
call :clean_maven_cache
call :rebuild_project
goto end

:check_env_cmd
call :check_java_version
call :check_maven_version
goto end

:all_cmd
call :check_java_version
call :check_maven_version
call :clean_project
call :clean_maven_cache
call :rebuild_project
goto end

:end
endlocal
